package com.daikin.ktmustwrite.ui.fragment.tree

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.daikin.ktmustwrite.R
import com.daikin.ktmustwrite.app.base.BaseFragment
import com.daikin.ktmustwrite.app.ext.*
import com.daikin.ktmustwrite.app.weight.recycleview.DefineLoadMoreView
import com.daikin.ktmustwrite.app.weight.recycleview.SpaceItemDecoration
import com.daikin.ktmustwrite.databinding.IncludeListBinding
import com.daikin.ktmustwrite.ui.adapter.AriticleAdapter
import com.daikin.ktmustwrite.viewmodel.request.RequestTreeViewModel
import com.daikin.ktmustwrite.viewmodel.state.TreeViewModel
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.android.synthetic.main.include_recyclerview.*

class AskFragment : BaseFragment<TreeViewModel, IncludeListBinding>() {
    private lateinit var loadSir: LoadService<Any>
    private val requestTreeViewModel:RequestTreeViewModel by viewModels()
    private lateinit var footView: DefineLoadMoreView
    private val ariticleAdapter:AriticleAdapter by lazy {
        AriticleAdapter(
            arrayListOf(),
            showTag = true
        )
    }

    override fun layoutId(): Int {
        return R.layout.include_list
    }

    override fun initView(savedInstanceState: Bundle?) {
        //状态页配置
        loadSir = loadServiceInit(swipeRefresh) {
            //点击重试时触发的操作
            loadSir.showLoading()
            requestTreeViewModel.getAskData(true)
        }
        requestTreeViewModel.getAskData(true)
        recyclerView.init(LinearLayoutManager(context),ariticleAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(8f)))
            footView=it.initFooter(SwipeRecyclerView.LoadMoreListener {
                requestTreeViewModel.getAskData(false)
            })
        }
        swipeRefresh.init {
            requestTreeViewModel.getAskData(true)
        }
        ariticleAdapter.run {

        }
    }

    override fun lazyLoadData() {
        loadSir.showLoading()
        requestTreeViewModel.getAskData(true)
    }

    override fun createObserver() {
        requestTreeViewModel.askDataState.observe(viewLifecycleOwner, Observer {
            loadListData(it,ariticleAdapter,loadSir,recyclerView,swipeRefresh)
        })
    }
}